from mbsh.core.images import *
from mbsh.core.models import SmallModel
from tests import show_img

__author__ = 'gpu'

import unittest

import os

from mbsh import create_app, g_temp_dir
import cv2


class ImagesTestCase(unittest.TestCase):
    def setUp(self):
        app = create_app(os.getenv('FLASK_CONFIG') or 'default')
        app.app_context().push()
        self.app = app

    def tearDown(self):
        print('tear down')
        # os.unlink(mbsh.app.config['DATABASE'])

    def test_get_file_name(self):
        self.assertEqual('01.0000000004692.0007.1482287232.jpg', cal_file_name('01.0000000004692.0007.1482287232.jpg'))

        self.assertTrue('01.0000000004692.0007.1482287232.jpg' != cal_file_name('早癌3.jpg'))
        self.assertTrue('胃早癌 (7)_看图王.jpg' != cal_file_name('胃早癌 (7)_看图王.jpg'))
        file_name = cal_file_name('DSC00013101.jpg')
        print(file_name)

    def test_read_to_pd(self):
        df = read_to_pd(r'E:\projects\znyx\znyx-model\tests\res\imgs')
        self.assertEqual(7, len(df['classname']))
        print(df.values.tolist())

    def test_read_img_from_url(self):
        img = read_img_from_url(r'https://www.baidu.com/img/bd_logo1.png')
        self.assertIsNotNone(img)

    def test_merge_to_one_fold(self):
        merge_to_one_fold(r'E:\projects\znyx\znyx-model\tests\res\crop\1', out_dir=g_temp_dir + '/test')

    def test_load_imgs(self):
        train_data, files = load_multi_img_data(r'E:\projects\znyx\znyx-model\tests\res\crop\1', 2)
        self.assertEqual(len(train_data), len(files))

    def test_load_img_data(self):
        files = fetch_all_files(r'E:\projects\znyx\znyx-model\tests\res\crop\1')
        train_data, files = load_img_data(files)
        self.assertEqual(len(train_data), len(files))

    def test_load_files(self):
        files = fetch_all_files(r'E:\projects\znyx\znyx-model\tests\res\crop\1', file_exts=['.jpg'])
        self.assertEqual(66, len(files))

    def test_resize_imgs(self):
        resize_multi_img_files(r'E:\projects\znyx\znyx-model\tests\res\crop\1',
                               g_temp_dir + '/test/resize', n_process=5)

    def test_read_img_base64(self):
        img = read_img_file(r'E:\projects\znyx\znyx-model\tests\res\imgs\0\01.0000000041263.0006.1512089085.jpg')
        retval, buffer = cv2.imencode('.jpg', img)
        base64_data = encode_base64_img(buffer)

        request_data = {'imgBase64': base64_data}
        img = read_img_from_base64(request_data)
        show_img(img)

        request_data['img_params'] = {'hue_rotate': -6, 'saturate': 6}
        img = read_img_from_base64(request_data)
        show_img(img)

    def test_classify(self):
        fold_path = r'E:\projects\znyx-admin\trunk\data\classify\stomach'
        file = ClassifyResultFile.init_file(fold_path)
        file = ClassifyResultFile.from_json(fold_path)
        result = file.result_list[0]
        self.assertIsNone(file.get_user_label(result.file_name, "a3"))
        file.update_user_label(result.file_name, "a3", 1)
        self.assertEqual(1, file.get_user_label(result.file_name, "a3").class_index)
        file.save_json()

        file = ClassifyResultFile.from_json(fold_path)
        result = file.result_list[0]
        self.assertEqual(1, file.get_user_label(result.file_name, "a3").class_index)
        file.update_user_label(result.file_name, "a3", 2)
        self.assertEqual(2, file.get_user_label(result.file_name, "a3").class_index)


if __name__ == '__main__':
    unittest.main()
